package com.example.lx6_4.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.entity.OrderEntityItem
import com.example.lx6_4.R
import com.example.lx6_4.databinding.ItemOrderBinding

class OrderAdapter : PagingDataAdapter<OrderEntityItem, OrderAdapter.OrderViewHolder>(diff){

    companion object{
        val diff = object : ItemCallback<OrderEntityItem>() {
            override fun areItemsTheSame(
                oldItem: OrderEntityItem,
                newItem: OrderEntityItem
            ): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(
                oldItem: OrderEntityItem,
                newItem: OrderEntityItem
            ): Boolean {
                return oldItem == newItem
            }

        }
    }

    class OrderViewHolder(view:View) : ViewHolder(view){
        val binding = DataBindingUtil.bind<ItemOrderBinding>(view)
    }

    override fun onBindViewHolder(holder: OrderViewHolder, position: Int) {
        holder.binding?.order = getItem(position)
        holder.itemView.setOnClickListener {
            ARouter.getInstance().build("/app/XqOrderActivity")
                .withInt("orderId",holder.binding?.order!!.id)
                .navigation()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrderViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_order, parent, false)
        return OrderViewHolder(view)
    }

}